//
//  RadarFooterView.m
//  carcareIOS
//
//  Created by wr on 16/2/29.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "RadarFooterView.h"

#define CENTER_VIEW_HEIGHT (83 * __MAIN_RATIO_375)
#define FOOTER_VIEW_HEIGHT (76 * __MAIN_RATIO_375)

@interface RadarFooterView()

{
    //虚线起点位置
    CGPoint dottenLineStart;
    CGPoint dottenLineEnd;
}

@property (nonatomic , copy) NSArray * headArr;

@property (nonatomic , copy) NSArray * centerArr;

@property (nonatomic , copy) NSString * startLocStr;

@property (nonatomic , copy) NSString * endLocStr;

@end

@implementation RadarFooterView

- (instancetype)initWithFrame:(CGRect)frame withHeadArr:(NSArray *)headArray withCenterArr:(NSArray *)centerArr withStartLocStr:(NSString *)startLocStr withEndLocStr:(NSString *)endLocStr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.headArr = headArray;
        self.centerArr = centerArr;
        self.startLocStr = startLocStr;
        self.endLocStr = endLocStr;
        
        [self createSubviews];
    }
    return self;
}
- (void)createSubviews
{
    //上半部分view
    UIView * headView = [[[UIView viewWithFrame:CGRectMake(0, 0, self.width, HEAD_VIEW_HEIGHT)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self];
    [[[UIView viewWithFrame:CGRectMake(0, headView.height - 0.5, self.width, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:headView];
    
    CGFloat headSmallViewY = 34 * __MAIN_RATIO_375;
    
    CGFloat leftX = 22 * __MAIN_RATIO_375;
    CGFloat viewAndViewX = 5;
    CGFloat headViewW = (self.width - leftX * 2 - viewAndViewX * (self.headArr.count - 1)) / self.headArr.count;
    CGFloat viewH = 53 * __MAIN_RATIO_375;
    for (int index = 0 ; index < self.headArr.count; index ++) {
        CGFloat viewX = leftX + (headViewW + viewAndViewX) * index;
        [self viewWithFrame:CGRectMake(viewX, headSmallViewY, headViewW, viewH) withSuperView:headView withTitle:[self.headArr[index] firstObject] withTitleFont:FONT_DEFAULT_Light(25 * __MAIN_RATIO_375)  withDetailTitle:[self.headArr[index] lastObject] withAlign:NSTextAlignmentCenter];
    }
    self.upAndDownImageV = [Factory imageViewWithCenter:CGPointMake(headView.width / 2, headSmallViewY / 2) image:ImageNamed(@"radarImageUp") onView:headView];
    self.upAndDownImageV.backgroundColor = [UIColor purpleColor];
    [headView setBackgroundColor:[UIColor grayColor]];
    CGFloat smallLineW = 95;
    self.smallLineView = [[[UIView viewWithFrame:CGRectMake((headView.width - smallLineW) / 2, headSmallViewY / 2 + ImageNamed(@"radarImageUp").size.height/2, smallLineW, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:headView];
    //创建头部的滑动button
    __weak __typeof(self) wself = self;
    UIButton * panGestureButton = [[UIButton buttonWithFrame:CGRectMake(0, 0, self.width, headView.height) title:@"" textColor:[UIColor clearColor] font:FONT_BUTTON click:^{
        if (wself.panGesClick) wself.panGesClick();
    }] setupOnView:headView];

    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesClick:)];
    [panGestureButton addGestureRecognizer:panGesture];
    
    //中心view
    UIView * centerView = [[[UIView viewWithFrame:CGRectMake(0, headView.bottom, __MAIN_WIDTH, CENTER_VIEW_HEIGHT)] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
    centerView.backgroundColor = [UIColor yellowColor];
    [[[UIView viewWithFrame:CGRectMake(0, centerView.height - 0.5, self.width, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:centerView];
    CGFloat centerSmallViewY = (centerView.height - viewH) / 2;
    CGFloat centerSmallViewW = (self.width - viewAndViewX * (self.centerArr.count - 1)) / self.centerArr.count;
    for (int index = 0; index < self.centerArr.count; index ++) {
        CGFloat viewX = (centerSmallViewW + viewAndViewX) * index;
        [self viewWithFrame:CGRectMake(viewX, centerSmallViewY, centerSmallViewW, viewH) withSuperView:centerView withTitle:[self.centerArr[index] firstObject] withTitleFont:FONT_DEFAULT_Light(23 * __MAIN_RATIO_375)  withDetailTitle:[self.centerArr[index] lastObject] withAlign:(index == 0 ? NSTextAlignmentCenter : (index == self.centerArr.count - 1 ? NSTextAlignmentCenter : NSTextAlignmentCenter ))];
    }
    
    //底部view
    UIView * footerView = [[[UIView viewWithFrame:CGRectMake(0, centerView.bottom, self.width, FOOTER_VIEW_HEIGHT)] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
    footerView.backgroundColor = [UIColor magentaColor];
    CGFloat footerViewX = 33 * __MAIN_RATIO_375;
    CGFloat footerLabelViewH = 45 * __MAIN_RATIO_375;
    CGFloat footerViewY =  ( footerView.height -  footerLabelViewH - 5) / 2;
    CGFloat footerLabelH = 20;
    CGFloat footerBottomLabelY = footerViewY + footerLabelH + (footerLabelViewH - footerLabelH * 2) + 5;
    CGFloat cornerViewH = 5;
    UIView * startCornerView = [self cornerViewWithColor:COLOR_HEXSTRING(@"#43E6AF") withFrame:CGRectMake(footerViewX, footerViewY + (footerLabelH - cornerViewH) / 2, cornerViewH, cornerViewH) onView:footerView];
    dottenLineStart = CGPointMake( footerViewX + cornerViewH / 2, HEAD_VIEW_HEIGHT + CENTER_VIEW_HEIGHT + startCornerView.bottom + 1);
    CGFloat footerLabelW = self.width - footerViewX * 2 - cornerViewH * 2 ;
    UILabel * footerTopLabel = [Factory labelWithFrame:CGRectMake(footerViewX + cornerViewH * 2, footerViewY, footerLabelW, footerLabelH) font:FONT_DEFAULT_Light(14  * __MAIN_RATIO_375) text:[NSString stringWithFormat:@"起： %@",self.startLocStr] textColor:[Theme wordColor] onView:footerView textAlignment:NSTextAlignmentLeft];
    
    UIView * endCornerView = [self cornerViewWithColor:[Theme orangeColor] withFrame:CGRectMake(footerViewX, footerBottomLabelY + (footerLabelH - cornerViewH) / 2, cornerViewH, cornerViewH) onView:footerView];
    dottenLineEnd = CGPointMake(dottenLineStart.x, HEAD_VIEW_HEIGHT + CENTER_VIEW_HEIGHT + endCornerView.top - 1);
    [self setNeedsDisplay];
    
    [Factory labelWithFrame:CGRectMake(footerTopLabel.left, footerBottomLabelY, footerLabelW, footerLabelH) font:FONT_DEFAULT_Light(14  * __MAIN_RATIO_375) text:[NSString stringWithFormat:@"终： %@",self.endLocStr] textColor:[Theme wordColor] onView:footerView textAlignment:NSTextAlignmentLeft];
    
}
- (void)panGesClick:(UIPanGestureRecognizer *)panGesture
{
    if (panGesture.state == UIGestureRecognizerStateBegan) if(self.panGesClick)self.panGesClick();    
}
#pragma mark - 创建小圆点  设置颜色
- (UIView *)cornerViewWithColor:(UIColor *)color withFrame:(CGRect)frame onView:(UIView *)superView
{
    UIView * cornerView = [[[UIView viewWithFrame:frame] resetBackgroundColor:color] setupOnView:superView];
    cornerView.layer.cornerRadius = cornerView.width / 2;
    cornerView.layer.masksToBounds = YES;
    return cornerView;
}
#pragma mark - 创建具有主标题 副标题的每个小view
- (void)viewWithFrame:(CGRect)frame withSuperView:(UIView *)superView withTitle:(NSString *)title withTitleFont:(UIFont *)titleFont withDetailTitle:(NSString *)detailTitle withAlign:(NSTextAlignment)textAlignment
{
    UIView * backView = [[[UIView viewWithFrame:frame] resetBackgroundColor:[UIColor redColor]] setupOnView:superView];
    backView.backgroundColor = [UIColor clearColor];
    CGFloat titleLabelH = 23;
    CGFloat detailLabelH = 20;
    [Factory labelWithFrame:CGRectMake(0, 0, backView.width, titleLabelH) font:titleFont text:title textColor:[Theme wordColor] onView:backView textAlignment:NSTextAlignmentCenter];
    CGFloat lableAndLabelY = backView.height - titleLabelH - detailLabelH;
    [Factory labelWithFrame:CGRectMake(0, titleLabelH + lableAndLabelY, backView.width, detailLabelH) font:FONT_DEFAULT_Light(13 * __MAIN_RATIO_375) text:detailTitle textColor:[Theme wordGrayNewColor] onView:backView textAlignment:textAlignment];
    
}
#pragma mark - 画虚线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, [Theme lineColor].CGColor);
    CGFloat lengths[] = {2 , 2 , 2, 2, 2 , 2 , 2, 2, 2, 2};
    CGContextSetLineDash(context, 0, lengths, 1);//跳过0个再画虚线
    CGContextMoveToPoint(context, dottenLineStart.x, dottenLineStart.y);
    CGContextAddLineToPoint(context, dottenLineEnd.x, dottenLineEnd.y);
    CGContextStrokePath(context);
    CGContextClosePath(context);
}
@end
